package org.dong.samples.web;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.dong.samples.model.ExamTest;
import org.dong.samples.model.ExamTestType;
import org.dong.samples.model.PagerModel;
import org.dong.samples.model.Score;
import org.dong.samples.model.Subject;
import org.dong.samples.model.vo.ExamTestVO;
import org.dong.samples.model.vo.MessageVO;
import org.dong.samples.model.vo.ScoreVO;
import org.dong.samples.service.ManageTesService;
import org.dong.samples.service.ScoreService;
import org.dong.samples.util.NowTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(value="/teacher")
public class ExamTestController {
	
	@Autowired
	private ManageTesService manageTesService;
	
	@Autowired
	private ScoreService scoreService;
	
	@ResponseBody
	@RequestMapping(value = "/getTestById", method = RequestMethod.POST)
	public ExamTestVO getTestById(HttpServletRequest request) throws Exception {
		String id= request.getParameter("testId");
		ExamTestVO resultVO = manageTesService.getTestById(Integer.parseInt(id));
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/saveExamTest", method = RequestMethod.POST)
	public MessageVO saveExamTest(HttpServletRequest request) throws Exception{
		String subId = request.getParameter("subId");
		String testType = request.getParameter("testType");
		String content = request.getParameter("content");
		String chooseA = request.getParameter("chooseA");
		String chooseB = request.getParameter("chooseB");
		String chooseC = request.getParameter("chooseC");
		String chooseD = request.getParameter("chooseD");
		String anwer = request.getParameter("anwer");
		ExamTest test = new ExamTest();
		ExamTestType type = new ExamTestType();
		type.setId(Integer.parseInt(testType));
		Subject sub = new Subject();
		sub.setSubId(Integer.parseInt(subId));
		test.setAnswer(anwer);
		test.setChooseA(chooseA);
		test.setChooseB(chooseB);
		test.setChooseC(chooseC);
		test.setChooseD(chooseD);
		test.setContent(content);
		test.setExamTestType(type);
		test.setSubject(sub);
		manageTesService.saveExamTest(test);
		MessageVO vo = new MessageVO();
		vo.setMassage("添加考题成功！");
		return vo;
	}
	
	@ResponseBody
	@RequestMapping(value = "/editExamTest", method = RequestMethod.POST)
	public MessageVO editExamTest(HttpServletRequest request) throws Exception{
		String id = request.getParameter("id");
		String subId = request.getParameter("subId");
		String testType = request.getParameter("testType");
		String content = request.getParameter("content");
		String chooseA = request.getParameter("chooseA");
		String chooseB = request.getParameter("chooseB");
		String chooseC = request.getParameter("chooseC");
		String chooseD = request.getParameter("chooseD");
		String anwer = request.getParameter("anwer");
		ExamTest test = new ExamTest();
		ExamTestType type = new ExamTestType();
		type.setId(Integer.parseInt(testType));
		Subject sub = new Subject();
		sub.setSubId(Integer.parseInt(subId));
		test.setAnswer(anwer);
		test.setChooseA(chooseA);
		test.setChooseB(chooseB);
		test.setChooseC(chooseC);
		test.setChooseD(chooseD);
		test.setContent(content);
		test.setExamTestType(type);
		test.setSubject(sub);
		test.setId(Integer.parseInt(id));
		manageTesService.editExamTest(test);
		MessageVO vo = new MessageVO();
		vo.setMassage("修改考题成功！");
		return vo;
	}
	

	@RequestMapping(value = "/examTest")
	public String toTest(Model model) throws Exception{
		PagerModel<ExamTestVO> testList = manageTesService.pageFindExamTest();
//		List<ExamTest> testList = manageTesService.loadAllTest();
		List<Subject> subList = manageTesService.findSubject();
		List<ExamTestType> typeList = manageTesService.findExamTestType();
		model.addAttribute("testList", testList);
		model.addAttribute("subList", subList);
		model.addAttribute("typeList", typeList);
		return "teacher/examTest";
	}

	@RequestMapping(value = "/testDelete/{id}", method = RequestMethod.GET)
	public String toDelete(RedirectAttributes redirectAttributes, Model model, @PathVariable Integer id){
		try {
			manageTesService.deleteTest(id);
			redirectAttributes.addFlashAttribute("message", "删除成功。");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/teacher/examTest";
	}
	
	@RequestMapping(value = "/studentResults", method = RequestMethod.GET)
	public String studentResults(Model model){
		List<ScoreVO> list = new ArrayList<ScoreVO>();
		model.addAttribute("list", list);
		return "teacher/studentResults";
	}
	
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	public String conditionalQuery(Model model, HttpServletRequest request) throws Exception{
		String subject = request.getParameter("subject");
		String userId = request.getParameter("userId");
		String userName = request.getParameter("userName");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		List<ScoreVO> list = new ArrayList<ScoreVO>();
		if(subject!=""&&userId==""&&userName==""&&startTime==""&&endTime==""){
			list = scoreService.getScoreBySubject(subject);
		}
		if(userId!=""&&subject==""&&userName==""&&startTime==""&&endTime==""){
			list = scoreService.getScoreByuserId(userId);
		}
		if(userName!=""&&subject==""&&userId==""&&startTime==""&&endTime==""){
			list = scoreService.getScoreByuserName(userName);
		}
		if(startTime!=""&&endTime!=""&&subject==""&&userId==""&&userName==""){
			list = scoreService.getScoreByTime(startTime, endTime);
		}
		if(subject==""&&userId==""&&userName==""&&startTime==""&&endTime==""){
			list = null;
		}
		model.addAttribute("list", list);
		return "teacher/studentResults";
	}
	
}
